package com.zhangdp.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.zhangdp.dto.Result;
import com.zhangdp.entity.ShopType;
import com.zhangdp.mapper.ShopTypeMapper;
import com.zhangdp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    public Result queryList() {
        Object resultJson = redisTemplate.opsForValue().get("shopTypeList");
        if (resultJson != null) {
            return Result.ok(JSONUtil.toList(resultJson.toString(), ShopType.class));
        }
        List<ShopType> typeList = this
                .query().orderByAsc("sort").list();
        if (CollUtil.isEmpty(typeList)) {
            return Result.fail("无类型信息！");
        }
        redisTemplate.opsForValue().set("shopTypeList", JSONUtil.toJsonStr(typeList));
        return Result.ok(typeList);
    }
}
